package com.xquant.pricing.pricingConfig.controller;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageInfo;
import com.xquant.common.security.utils.SecurityUtils;
import com.xquant.pricing.pricingConfig.entity.PricUserGroup;
import com.xquant.pricing.pricingConfig.entity.User;
import com.xquant.pricing.pricingConfig.mapper.UserMapper;
import com.xquant.pricing.pricingConfig.service.PricingUserGroupService;
import com.xquant.pricing.utils.CommResponse;
import com.xquant.pricing.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.xquant.common.core.utils.PageUtils.startPage;


/**
 * @Auther: newCould
 * @Date: 01/03/2022 15:22
 * @Description: newCloud Controller  用户组管理
 */
@RestController
@RequestMapping("pricingUserGroup")
public class PricingUserGroupController {
    @Autowired
    private PricingUserGroupService pricingUserGroupService;
    @Autowired
    private UserMapper userMapper;

    /**
     * 查询 组合
     */
    @RequestMapping(value = "getPricUserGroupList.action")
    public PageInfo getPricUserGroupList(@RequestParam Map<String, Object> param) {
        startPage();
        PageInfo<PricUserGroup> pageInfo = pricingUserGroupService.getPricUserGroupList(param);
        return pageInfo;
    }

    /**
     * 查询 组合 根据ID
     */
    @RequestMapping(value = "getPricUserGroupById.action")
    public CommResponse getPricUserGroupById(@RequestParam Map<String, Object> param) {
        startPage();
        List<PricUserGroup> list = pricingUserGroupService.getPricUserGroupById(param);
        return CommResponse.success(list.get(0));
    }

    /**
     * 查询 组合内
     */
    @RequestMapping(value = "findInUserList.action")
    public PageInfo findInUserList(@RequestParam Map<String, Object> param) {
        startPage();
        PageInfo<User> pageInfo = pricingUserGroupService.findInUserList(param);
        return pageInfo;
    }

    /**
     * 查询 组合内
     */
    @RequestMapping(value = "findUserList.action")
    public PageInfo findUserList(@RequestParam Map<String, Object> param) {
        startPage();
        PageInfo<User> pageInfo = pricingUserGroupService.findUserList(param);
        return pageInfo;
    }

    /**
     * 查询 组合外
     */
    @RequestMapping(value = "findOutUserList.action")
    public PageInfo findOutUserList(@RequestParam Map<String, Object> param) {
        startPage();
        PageInfo<User> pageInfo = pricingUserGroupService.findOutUserList(param);
        return pageInfo;
    }

    /**
     * 新增组合
     */
    @RequestMapping(value = "/addPricUserGroup.action")
    public CommResponse addPricUserGroup(@RequestParam Map<String, Object> param) {
        param.put("userId", SecurityUtils.getUserId());
        List<PricUserGroup> list = pricingUserGroupService.queryPricUserGroup(param);
        if (list.size() > 0) {
            return CommResponse.failure("组合已存在，请检查！");
        }
        param.put("groupSort", DateUtils.getCurrentDay());
        pricingUserGroupService.addPricUserGroup(param);
        return CommResponse.success("操作成功");
    }

    /**
     * 修改组合
     */
    @RequestMapping(value = "/updatePricUserGroup.action")
    public CommResponse updatePricUserGroup(@RequestParam Map<String, Object> param) {
        param.put("userId", SecurityUtils.getUserId().toString());
        List<PricUserGroup> list = pricingUserGroupService.queryPricUserGroup(param);
        if (list.size() > 1) {
            return CommResponse.failure("组合已存在，请检查！");
        }
        if (list.size() == 1) {
            if (!list.get(0).getGroupId().equals(param.get("groupId"))) {
                return CommResponse.failure("组合已存在，请检查！");
            }
        }

        pricingUserGroupService.updatePricUserGroup(param);
        return new CommResponse(true, "操作成功");
    }

    /**
     * 删除组合
     */
    @RequestMapping(value = "/deletePricUserGroup.action")
    public CommResponse deletePricUserGroup(@RequestParam Map<String, Object> param) {
        param.put("userId", SecurityUtils.getUserId());
        if (StrUtil.isNotBlank(MapUtil.getStr(param, "groupIds"))) {
            JSONArray jsonArray = JSONUtil.parseArray(MapUtil.getStr(param, "groupIds"));
            jsonArray.get(0);
            for (int i = 0; i < jsonArray.size(); i++) {
                param.put("groupId", jsonArray.get(i));
                pricingUserGroupService.deletePricUserGroup(param);
            }
        } else {
            pricingUserGroupService.deletePricUserGroup(param);
        }
        return new CommResponse(true, "操作成功");
    }

    /**
     * 移入 组合
     */
    @RequestMapping(value = "/dealTransferUser.action")
    public CommResponse dealTransferUser(@RequestParam Map<String, Object> param) {

        String groupId = param.get("groupId").toString();
        JSONArray jsonArray = JSONUtil.parseArray(param.get("userIds"));
        List userIdList = new ArrayList();

        for (int i = 0; i < jsonArray.size(); i++) {
            param.put("userId", jsonArray.get(i));
            userIdList.add(jsonArray.get(i));
            List<User> list = pricingUserGroupService.queryPricUserGroupBy(param);
            for (int j = 0; j < list.size(); j++) {
                User user = list.get(j);
                if (StrUtil.isNotBlank(user.getPricGroup())) {
                    if (Integer.parseInt(user.getGroupExist()) == 0) {
                        pricingUserGroupService.upPricUserGroup(param);
                    }
                } else {
                    pricingUserGroupService.upPricUserGroup02(param);
                }

            }
        }
        Map map = new HashMap();
        if (userIdList.size() > 0) {
            map.put("userIds", userIdList);
        }
        List<User> userList = userMapper.findUserByUserIds(map);
        for (int i = 0; i < userList.size(); i++) {
            map.put("userId", userList.get(i).getUserId());
            map.put("groupId", groupId);
            List<User> list = pricingUserGroupService.queryPricUserGroupBy(map);
            for (int j = 0; j < list.size(); j++) {
                User user = list.get(j);
                if (StrUtil.isNotBlank(user.getPricGroup())) {
                    if (Integer.parseInt(user.getGroupExist()) > 0) {
                        user.setPricGroup(user.getPricGroup().replace(groupId, "").replace(",,", ","));
                        if (user.getPricGroup().startsWith(",")) {
                            user.setPricGroup(user.getPricGroup().substring(1, user.getPricGroup().length()));
                        }
                        if (user.getPricGroup().endsWith(",")) {
                            user.setPricGroup(user.getPricGroup().substring(0, user.getPricGroup().length() - 1));
                        }
                        map.put("groupId", user.getPricGroup());
                        pricingUserGroupService.upPricUserGroup02(map);
                    }
                }
            }

        }
        return new CommResponse(true, "操作成功");
    }

    /**
     * 移入 组合
     */
    @RequestMapping(value = "/upPricUserGroup.action")
    public CommResponse upPricUserGroup(@RequestParam Map<String, Object> param) {
        param.put("userId", SecurityUtils.getUserId());
        JSONArray jsonArray = JSONUtil.parseArray(param.get("data"));
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = JSONUtil.parseObj(jsonArray.get(i));
            Map map = (Map) jsonObject;
            param.putAll(map);
            int gpCount = pricingUserGroupService.queryPricUserGroupCount(param);
            if (gpCount > 0) {
                pricingUserGroupService.upPricUserGroup(param);
            } else {
                pricingUserGroupService.upPricUserGroup02(param);
            }

        }
        return new CommResponse(true, "操作成功");
    }

    /**
     * 移出 组合
     */
    @RequestMapping(value = "/downPricUserGroup.action")
    public CommResponse downPricUserGroup(@RequestParam Map<String, Object> param) {
        param.put("userId", SecurityUtils.getUserId());
        JSONArray jsonArray = JSONUtil.parseArray(param.get("data"));
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = JSONUtil.parseObj(jsonArray.get(i));
            Map map = (Map) jsonObject;
            param.putAll(map);
            pricingUserGroupService.downPricUserGroup(param);
        }
        return new CommResponse(true, "操作成功");
    }

}
